#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    socket = new QTcpSocket;
    socket->connectToHost(QHostAddress("192.168.1.233"), 8888);

    connect(ui->sendButton, SIGNAL(clicked(bool)), this, SLOT(sendButton()));
    connect(ui->exitButton, SIGNAL(clicked(bool)), this, SLOT(exitButton()));
    connect(socket, SIGNAL(readyRead()), this, SLOT(recvInfo()));
}

Widget::~Widget()
{
    delete ui;
}

void Widget::sendButton()
{
    QString str1;
    str1 = ui->clientEdit->toPlainText();
    qDebug() << str1 << endl;

    ui->clientShow->append("client:");
    ui->clientShow->append(" "+str1);

    QByteArray buff = str1.toUtf8();
    socket->write(buff);
    ui->clientEdit->clear();
}

void Widget::exitButton()
{
    QByteArray buff1 = "client exit";
    socket->write(buff1);
    this->close();
}

void Widget::recvInfo()
{
    QString buf = socket->readAll();

    ui->clientShow->append("server:");
    ui->clientShow->append(" "+buf);
}

